<?php
namespace app\my\service;
use app\my\model\MyFriendModel;
use app\my\validata\MyValidata as mv;
use app\base\service\BaseService;
use think\facade\Log;
class MyFriendService extends BaseService 
{
	protected $friend ='';
	protected $mv='';
	 public function __construct()
	 {
	 	parent::__construct();
	 	$this->friend = new MyFriendModel;
	 	$this->mv = new mv;
	 }

	 //添加好友
	 
	 public function insert_friend($data){
	 	if (!$this->mv->scene('openid')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
	 	$openid1= $this->user['Openid'];//主添加人
	 	$openid2 = $data['Openid'];//被添加人的openid
	 	//判断是否开启了好友添加
	 	$userinfo = $this->get_userinfo($openid2);
	 	if($userinfo['power'] == 1)  return $this->return_code(305,'对不起，此好友为不可添加好友');
	 	//判断是否是好友关系
	 	$where['openid1']=$openid1;
	 	$where['openid2']=$openid2;
	 	$bool = $this->friend->countWhere('user_friend',$where);
	 	if($bool>0)return $this->return_code(301,'已经是你好友了');
	 	$friend = array('openid1' => $openid1,'openid2'=>$openid2,'createtime'=>time(),'status'=>0);
	 	$code = $this->friend->insert('user_friend',$friend);
	 	if($code) return $this->return_code(200,'成功');
	    return $this->return_code(201,'添加好友失败');

	 }
	 
	//s是否同意好友
	 public function agree($data){
	 	if (!$this->mv->scene('friendagree')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
	 	$where['friendid'] = $data['friendid'];
	 	$friend['status'] =$data['status'];
	 	$msg = '';
	 	switch ($data['status']) {
	 		case '0':
	 			$msg = '未处理';
	 			break;
	 		case '1':
	 			$msg = '已同意';
	 			break;
	 		case '2':
	 			$msg = '已拒绝';
	 			break;
	 		default:
	 			$msg = '已拒绝';
	 			break;
	 	}
	 	$code = $this->friend->updateByWhere('user_friend',$where,$friend);
	 	if($code) return $this->return_code(200,$msg);
	    return $this->return_code(201,'失败');

	 }

	//查看好友列表
	 public function friend_list($data)
	 {
	 	$res = $this->friend->freind_list($this->user['Openid'],$data);
	    return $this->return_code(200,"获得好友列表成功",$res);
	 }

	//删除好友
	






}